package trastienda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import trastienda.excepcion.DAOExcepcion;
import trastienda.modelo.Salas;
import trastienda.util.ConexionBD;


public class SalasDAO extends BaseDAO {

	public Salas obtener(int idSala) throws DAOExcepcion {
	    Salas vo = new Salas();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idSala, nomSala, ubicacion, direccion, capMax,caracteristicas from salas where idSala=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idSala);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdSala(rs.getInt(1));
				vo.setNomSala(rs.getString(2));
				vo.setUbicacion(rs.getString(3));
				vo.setDireccion(rs.getString(4));
				vo.setCapMax(rs.getInt(5));
				vo.setCaracteristicas(rs.getString(6));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<Salas> listar() throws DAOExcepcion {
		Collection<Salas> c = new ArrayList<Salas>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idSala,nomSala,ubicacion, direccion, capMax, caracteristicas from categoria order by nomSala";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Salas vo = new Salas();
				vo.setIdSala(rs.getInt("idSala"));
				vo.setNomSala(rs.getString("nomSala"));
				vo.setUbicacion(rs.getString("ubicacion"));
				vo.setDireccion(rs.getString("direccion"));
				vo.setCapMax(rs.getInt("capMax"));
				vo.setCaracteristicas(rs.getString("Caracteristicas"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	
	
	public Collection<Salas> buscarPornomSala(String nomSala)
			throws DAOExcepcion {
		String query = "select idSala, nomSala, ubicacion,  from Salas where (nomSala or ubicacion) like ? ";
		Collection<Salas> lista = new ArrayList<Salas>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nomSala+ "%");
			rs = stmt.executeQuery();
			
			while (rs.next()) {
				Salas vo = new Salas();
				vo.setIdSala(rs.getInt("idSala"));
				vo.setNomSala(rs.getString("nomSala"));
				vo.setUbicacion(rs.getString("Ubicacion"));
				
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	
	public Salas insertar(Salas vo) throws DAOExcepcion {
		String query = "insert into Salas(nomSala, ubicacion, direccion, capMax,caracteristicas) values (?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNomSala());
			stmt.setString(2, vo.getUbicacion());
			stmt.setString(3, vo.getDireccion());
			stmt.setInt(4, vo.getCapMax());
			stmt.setString(5, vo.getCaracteristicas());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdSala(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Salas actualizar(Salas vo) throws DAOExcepcion {
		String query = "update Salas set nomSala=?,ubicacion=?, direccion=? capMax=?, caracteristicas=? where idSala=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNomSala());
			stmt.setString(2, vo.getUbicacion());
			stmt.setString(3, vo.getDireccion());
			stmt.setInt(4, vo.getCapMax());
			stmt.setString(5, vo.getCaracteristicas());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	
}

